// package: learn-go
// filename: main
// author: Administrator@gmail.com
// time: 2023/8/9 16:51
package main

import (
	"bufio"
	"fmt"
	"io"
	"os/exec"
	"time"
)

func main() {
	command := "dir"
	params := []string{"/s"}
	//执行cmd命令: dir /s
	execCommandWin(command, params)
}

func execCommandWin(commandName string, params []string) bool {
	cmd := exec.Command(commandName, params...)
	//显示运行的命令
	fmt.Println(cmd.Args)
	stdout, err := cmd.StderrPipe()
	if err != nil {
		fmt.Println(err)
		return false
	}
	cmd.Start()

	reader := bufio.NewReader(stdout)
	//实时循环读取输出流中的一行内容
	for {
		line, err2 := reader.ReadString('\n')
		if err2 != nil || io.EOF == err2 {
			break
		}
		time.Sleep(1 * time.Second)
		fmt.Println(line)
	}

	cmd.Wait()
	return true
}
